package com.hb.trade.api.im.chat.manager;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.hb.core.pojo.center.au.dto.OrgDTO;
import com.hb.core.pojo.center.au.entity.User;
import com.hb.trade.client.center.OrgClient;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.fzzn.core.model.msg.Message;
import org.fzzn.core.model.user.layim.LayImUser;

/**
 * 构建layim user防腐层对象
 *
 * @author zhaojk
 */
@Slf4j
@Setter
@Getter
public class TradeLayImUser extends LayImUser {
    /**
     * 聊天内容
     */
    private String content;

    public final static String DEFAULT_AVATAR =
            "https://ddzb-wx.oss-cn-beijing.aliyuncs.com/avatar-default.png";

    /**
     * @param user 用户信息
     * @param mine true: 个人面板中的用户信息, false: 好友列表中的用户信息
     */
    public TradeLayImUser(User user, boolean mine) {

        log.debug("TradeLayImUser init start user is [{}], mine is [{}]", user, mine);
        BeanUtil.copyProperties(user, this);
        this.setStatus("online");
        this.setAvatar(DEFAULT_AVATAR);

        OrgClient orgClient = SpringUtil.getBean(OrgClient.class);
        Message<OrgDTO> org = orgClient.getOrgById(user.getOrgId());

        String orgName = "未加入企业";
        if (org.isSuccess()) {
            orgName = org.getData().getOrgName();
        }

        if (mine) {
            this.setUsername(user.getFullName());
            this.setSign(orgName);
        } else {
            this.setUsername(orgName);
        }

        this.setUname(user.getUsername());
    }

    /**
     * 好友列表中的用户信息
     *
     * @param user
     */
    public TradeLayImUser(User user) {
        this(user, false);
    }
}
